package pw.hais.zongcaibook.app;

import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.MenuItem;

import com.avos.avoscloud.AVAnalytics;

import de.greenrobot.event.EventBus;
import pw.hais.app.AppBaseActivity;
import pw.hais.zongcaibook.entity.EventT;
import pw.hais.zongcaibook.model.CheckAPPModel;
import pw.hais.zongcaibook.ui.UpdateAppWindows;

/**
 * Created by hais1992 on 2016/5/22/022.
 */
public class BaseActivity extends AppBaseActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (Build.VERSION.SDK_INT >= 21) {
            getSupportActionBar().setElevation(0);
        }
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    protected void onPause() {
        super.onPause();
        AVAnalytics.onPause(this);
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        AVAnalytics.onResume(this);
        EventBus.getDefault().register(this);
    }

    //收到更新
    public void onEventMainThread(EventT eventT) {
        switch (eventT.type){
            case UpdateApp:
                updateApp(eventT.obj);
                break;
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void updateApp(final Object obj) {
        final UpdateAppWindows updateAppWindows = new UpdateAppWindows(this);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                updateAppWindows.showCenter((CheckAPPModel)obj);
            }
        }, 1000);
    }
}
